﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using DECOR.BL.BC;
using DECOR.BL.BE;

namespace DECOR.WS
{
    /// <summary>
    /// Summary description for ClienteWS
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ClienteWS : System.Web.Services.WebService
    {


        [WebMethod(EnableSession = true)]
        public List<GenericBE> listaCanalDist()
        {

            GenericBE objBlank = new GenericBE();
            List<GenericBE> lst = null;
            try
            {
                if (Session["listaOrgVtas"] == null)
                {
                    Session.Timeout = 5;
                    objBlank.Codigo = ""; objBlank.Descripcion = "Todos";
                    lst = ClienteBC.Instance.listaCanalDist("", "");
                    lst.Add(objBlank);

                    Session.Add("listaCanalDist", lst);
                }
                else
                {

                    lst = (List<GenericBE>)Session["listaCanalDist"];

                }
            }
            catch
            {
                Session.Remove("listaCanalDist");
            }

            return lst;
        }

        [WebMethod(EnableSession = true)]
        public List<GenericBE> listar()
        {
            GenericBE objBlank = new GenericBE();
            List<GenericBE> lst = null;
            try
            {
                if (Session["listaOrgVtas"] == null)
                {

                    Session.Timeout = 5;
                    //objBlank.Codigo = ""; objBlank.Descripcion = "Todos";
                    lst = OficinaBC.Instance.listar();
                    //lst.Add(objBlank);

                    Session.Add("listar", lst);
                }
                else {

                    lst = (List<GenericBE>)Session["listar"];
                
                }
            }
            catch
            {
                Session.Remove("listaCanalDist");

            }

            return lst;


        }


        [WebMethod(EnableSession = true)]
        public List<GenericBE> listaOrgVtas()
        {
            GenericBE objBlank = new GenericBE();
            List<GenericBE> lst = null;
            try
            {
                if (Session["listaOrgVtas"] == null)
                {
                    Session.Timeout = 5;
                    objBlank.Codigo = ""; objBlank.Descripcion = "Todos";
                    lst = ClienteBC.Instance.listaOrgVtas("");
                    lst.Add(objBlank);

                    Session.Add("listaOrgVtas", lst);
                }
                else {

                    lst = (List<GenericBE>)Session["listaOrgVtas"];
                }
            }
            catch
            {
                Session.Remove("listaCanalDist");

            }

            return lst;

        }

        [WebMethod]
        public string login(string pUsu, string pPass)
        {
            string msj = string.Empty;
            
            try
            {
                UsuarioWebBE objUsuarioWeb = new UsuarioWebBE();
                objUsuarioWeb.Usuario = pUsu;
                objUsuarioWeb.Clave = pPass;

                string retval = UsuarioBC.Instance.login(ref objUsuarioWeb);

                if (string.IsNullOrEmpty(retval))
                {
                    if (string.IsNullOrEmpty(objUsuarioWeb.UsuarioSap) || string.IsNullOrEmpty(objUsuarioWeb.ClaveSap))
                    {
                        msj = "No cuenta con un Usuario SAP asignado.";
                    }
                    else
                    {
                        bool verifPswd = ParametroBC.contraseniaValida(objUsuarioWeb.Clave);
                        if (verifPswd)
                        {
                            msj = "Exito";
                        }
                        else
                        {
                            msj = "Pass o usu incorrecto.";
                        }
                    }
                }
                else
                {
                    msj = retval;
                }
            }
            catch (Exception ex)
            {
                msj = ex.Message.Replace("\\n", "<br />");
            }

            return msj;
        }

    }
}
